﻿using System;
using System.Collections.Generic;
using System.Transactions;
using Bingosoft.Data;

namespace Bingosoft.Modules.SecurityConsole.Privilege
{
    public class PrivilegeService
    {
        private readonly Dao _dao = Dao.Get();

        /// <summary>
        /// 删除模块
        /// </summary>
        /// <param name="moduleId">模块Id</param>
        public void DeleteModule(string moduleId)
        {
            DeletePrivilege(moduleId);
        }

        /// <summary>
        /// 删除功能
        /// </summary>
        /// <param name="functionId">功能Id</param>
        public void DeleteFunction(string functionId)
        {
            DeletePrivilege(functionId);
        }

        /// <summary>
        /// 批量删除操作
        /// </summary>
        /// <param name="operationIdList">操作Id数组</param>
        public void DeleteOperations(string[] operationIdList)
        {
            using (TransactionScope transactionScope = new TransactionScope(TransactionScopeOption.Required))
            {
                foreach (String operationId in operationIdList)
                {
                    DeletePrivilege(operationId);
                }
                transactionScope.Complete();
            }
        }

        /// <summary>
        /// 删除单个操作
        /// </summary>
        /// <param name="operationId">操作Id</param>
        public void DeleteOperation(string operationId)
        {
            DeletePrivilege(operationId);
        }

        /// <summary>
        /// 删除模块、功能或者操作
        /// </summary>
        /// <param name="privilegeId">模块、功能或操作ID</param>
        private void DeletePrivilege(string privilegeId)
        {
            using (TransactionScope transactionScope = new TransactionScope(TransactionScopeOption.Required))
            {
                _dao.ExecuteNonQuery("privilege.function.delete",
                                                        new Dictionary<string, object> { { "Id", privilegeId } });
                transactionScope.Complete();
            }
        }

        /// <summary>
        /// 保存模块、功能或操作
        /// </summary>
        /// <param name="module">模块、功能或操作对象</param>
        public void SaveOrUpdateModule(Module module)
        {
            using (TransactionScope transactionScope = new TransactionScope(TransactionScopeOption.Required))
            {
                if (string.IsNullOrEmpty(module.Parent))
                {
                    module.Parent = null;
                }

                _dao.ExecuteNonQuery(
                    String.IsNullOrEmpty(module.Id) ? "privilege.module.insert" : "privilege.module.update", module);
                transactionScope.Complete();
            }
        }

        /// <summary>
        /// 删除权限下的规则
        /// </summary>
        /// <param name="permissionRuleId">权限规则Id</param>
        public void DeletePermissionRule(string permissionRuleId)
        {
            using (TransactionScope transactionScope = new TransactionScope(TransactionScopeOption.Required))
            {
                IDictionary<string, string> parmaeters =
                        new Dictionary<string, string> { { "Id", permissionRuleId } };
                _dao.ExecuteNonQuery("privilege.operation.rule.deleteRoleRule", parmaeters);
                _dao.ExecuteNonQuery("privilege.operation.rule.delete", parmaeters);
                transactionScope.Complete();
            }
        }

        /// <summary>
        /// 批量删除权限规则
        /// </summary>
        /// <param name="ruleIds">权限规则Id集合</param>
        public void DeletePermissionRules(string[] ruleIds)
        {
            using (TransactionScope transactionScope = new TransactionScope(TransactionScopeOption.Required))
            {
                foreach (string ruleId in ruleIds)
                {
                    DeletePermissionRule(ruleId);
                }
                transactionScope.Complete();
            }
        }

        /// <summary>
        /// 检查模块、功能或操作编号的唯一性
        /// </summary>
        /// <param name="operationCode">模块、功能或操作编号</param>
        /// <returns></returns>
        public Boolean CheckUniqueOperation(string operationCode)
        {
            using (TransactionScope transactionScope = new TransactionScope(TransactionScopeOption.Suppress))
            {
                Boolean isUnique = _dao.QueryScalar<int>("privilege.operation.checkOperationCoceExists",
                                                         new Dictionary<string, string> { { "Code", operationCode } }) > 0;
                transactionScope.Complete();
                return isUnique;
            }
        }
    }
}